import { fileURLToPath, URL } from 'node:url'

import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
// export default defineConfig({
//   plugins: [
//     vue(),
//     vueDevTools(),
//   ],
//   resolve: {
//     alias: {
//       '@': fileURLToPath(new URL('./src', import.meta.url))
//     }
//   }
// })
export default defineConfig(({mode, command}) => {
  const env = loadEnv(mode, process.cwd());
  const { VITE_APP_ENV } = env;
  return {
    base: VITE_APP_ENV === 'production' ? '/' : '/',
    plugins: [
      vue({
        script: {
          defineModel: true
        }
      }),
      vueDevTools()
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      },
      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue', '.css', '.scss']
    },
    css: {
      preprocessorOptions: {
        scss: {
          javascriptEnabled: true,
          // 全局引入主题色scss文件
          additionalData: "@import '@/assets/styles/theme.scss';"
        }
      }
    },
    server: {
      host: '0.0.0.0',
      https: false,
      cors: true,
      open: true,
      proxy: {
        '/api': {
          // target: 'http://192.168.137.97:8080',
          // 后端
          target: 'http://192.168.123.53:8081',
          // 线上
          // target: 'http://192.168.123.62:8082',
          // target: 'http://192.168.137.1:8081',

          changeOrigin: true,
          // 后端
          rewrite: (p) => p.replace(/^\/api/, '')
          // 线上
          // rewrite: (p) => p.replace(/^\/api/, '/api')
        }
      }
    }
  }
})